package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.entity.Admin;
import cn.kgc.kgde.bookstore.entity.Cart;
import cn.kgc.kgde.bookstore.entity.ResultInfo;
import cn.kgc.kgde.bookstore.entity.User;
import cn.kgc.kgde.bookstore.mapper.UserMapper;
import cn.kgc.kgde.bookstore.service.CartService;
import cn.kgc.kgde.bookstore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2022-05-22
 */
@CrossOrigin
@Configuration
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private  UserService userService;
    @Resource
    private CartService cartService;
    @Resource
    private UserMapper userMapper;
    //查询用户列表 -- 后台 --  显示用户列表
    @GetMapping("/showUser")
    public ResultInfo showUser(User user, @RequestParam(defaultValue = "1") Integer pageNum, Integer pageSize){
        IPage<User> userList = userService.findUser( user ,pageNum,15);
        if (userList.getRecords()==null){
            return new ResultInfo(500,"查询用户出现错误··！");
        }
        return new ResultInfo( 200,"查询用户成功",userList);
    }

    //用户注册
    @PostMapping("/register")
    public ResultInfo register(User user){
        this.userService.save(user);
        //在注册时新建购物车
        Cart cart =new Cart();
        cart.setUserCode(user.getUserCode());
        this.cartService.save(cart);
        return new ResultInfo(200,"用户注册成功",user+"\n"+cart);
    }

    //登陆
    @PostMapping("/login")
    public ResultInfo login(String userCode, String userPassword, HttpSession session){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("*")
                .eq("userCode",userCode)
                .eq("userPassword",userPassword);
        User user=this.userService.getOne(queryWrapper);
        System.out.println("user = " + user);
        if(user!=null){
            session.setAttribute("userCode",user);
            return new ResultInfo(200,"登录成功",user);
        }
        return new ResultInfo(500,"登录失败");
    }
    //退出
    @GetMapping("/logout")
    public ResultInfo logout(HttpSession session){
        session.invalidate();
        return new ResultInfo(200,"已退出");
    }

    //前台 查询用户信息 根据 userCode
    @GetMapping("/chaUser")
    public ResultInfo chaUser(String userCode){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userCode",userCode);
        List<User> userList = this.userMapper.selectList(queryWrapper);
        System.out.println("userList = " + userList);
        return new ResultInfo(200,"查询成功",userList);
    }
}
